import * as React from 'react'
import {connect} from 'react-redux'

import {Session,Store} from '../store/types'
import actions from '../store/actions/session'
import {Redirect, Route} from 'react-router-dom'

interface Props{
  user:any, //当前用户
  path:string, // 匹配路径
  component:any,// 要渲染的组件
  // pathname:string
}

// 受保护的路由 高阶组件 只实现权限判断
class PrivateRoute extends React.Component<Props> {
 
  render() { 
    let {user,path,component:Component} = this.props 

    return (
      <Route path={path} render={
        (props)=>user?<Component {...this.props}/>:<Redirect to="/login"></Redirect>
      }/>
    )
  }
}
 
export default connect(
  (state:Store):Session => state.session,
  actions
)(PrivateRoute) ;